﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class user_index : System.Web.UI.Page
{

    private ProductBus ProductBus = new ProductBus();
    private int cat_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
        }
        if (Request.QueryString.Count > 0)
        {
            cat_id = Convert.ToInt32(Request.QueryString["categoryID"]);
            LoadProductByCategory(cat_id);

        }
        LoadAllProduct();

    }
    public void LoadAllProduct()
    {
        ClPageProduct.BindToControl = dtListProduct;
        ClPageProduct.DataSource = ProductBus.GetAllProduct().DefaultView;

        dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
        dtListProduct.DataBind();
    }

    public void LoadProductByCategory(int id)
    {
        ClPageProduct.BindToControl = dtListProduct;
        ClPageProduct.DataSource = ProductBus.GetProductByCategory(id).DefaultView;

        dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
        dtListProduct.DataBind();
    }

    protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "AddToCart")
        {
            //lay ID
            int idProduct = Convert.ToInt32(e.CommandArgument);
            //Them vao gio hang
            ShoppingCart.Instance.AddItem(idProduct);

            //tang so luot mua            
            ProductBus productBus = new ProductBus();
            int currentOrdernum = (int)productBus.GetOrderNum(idProduct).Rows[0]["pd_ordernum"];
            int newOrdernum = currentOrdernum + 1;
            productBus.UpdateOrderNum(idProduct, newOrdernum);
            //chuyen tiep den trang hien thi gio hang
            Response.Redirect("shoppingcart.aspx");
        }
    }
    protected void imgSearch_Click(object sender, ImageClickEventArgs e)
    {

        if (TextBox1.Text.Length > 0)
        {
            //SEARCH BY NAME
            System.Threading.Thread.Sleep(1000);
            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = ProductBus.SearchProduct(TextBox1.Text).DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
            //
        }
        if (drtpricefrom.SelectedIndex != 0 && drpriceto.SelectedIndex != 0)
        {
            //tim theo gia tien tu x den y
            Decimal price1 = Convert.ToDecimal(drtpricefrom.SelectedItem.Text);
            Decimal price2 = Convert.ToDecimal(drpriceto.SelectedItem.Text);

            System.Threading.Thread.Sleep(1000);
            DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daProduct.Search1(ds2.tbl_product, price1, price2);
            DataTable dt = new DataTable();
            dt = ds2.tbl_product;

            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = dt.DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }

        if (TextBox1.Text.Length > 0 && drtpricefrom.SelectedIndex != 0 && drpriceto.SelectedIndex != 0 && drcategory.SelectedIndex != 0)
        {
            //tim theo gia tien tu x den y va theo ten va theo ID
            Decimal price1 = Convert.ToDecimal(drtpricefrom.SelectedItem.Text);
            Decimal price2 = Convert.ToDecimal(drpriceto.SelectedItem.Text);
            String name = TextBox1.Text;

            CategoryDAL cateDAL = new CategoryDAL();
            int catID = Convert.ToInt32(cateDAL.GetCategoryID(drcategory.SelectedItem.Text).Rows[0]["cat_id"]);


            DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daProduct.Search3(ds2.tbl_product, price1, price2, name, catID);
            DataTable dt = new DataTable();
            dt = ds2.tbl_product;

            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = dt.DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }
        if (TextBox1.Text.Length > 0 && drtpricefrom.SelectedIndex != 0 && drpriceto.SelectedIndex != 0)
        {
            //tim theo gia tien tu x den y va theo ten
            Decimal price1 = Convert.ToDecimal(drtpricefrom.SelectedItem.Text);
            Decimal price2 = Convert.ToDecimal(drpriceto.SelectedItem.Text);
            String name = TextBox1.Text;
            System.Threading.Thread.Sleep(1000);
            DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daProduct.Search2(ds2.tbl_product, price1, price2, name);
            DataTable dt = new DataTable();
            dt = ds2.tbl_product;

            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = dt.DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }
        else if (TextBox1.Text.Length > 0 && drcategory.SelectedIndex != 0)
        {
            //tim theo nhom va ten

            String name = TextBox1.Text;

            CategoryDAL cateDAL = new CategoryDAL();
            int catID = Convert.ToInt32(cateDAL.GetCategoryID(drcategory.SelectedItem.Text).Rows[0]["cat_id"]);

            System.Threading.Thread.Sleep(1000);
            DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daProduct.Search5(ds2.tbl_product, catID, name);
            DataTable dt = new DataTable();
            dt = ds2.tbl_product;

            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = dt.DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }
    }

  
    protected void DropDownListSoryBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownListSoryBy.SelectedIndex == 0)
        {
            //ALL PRODUCT
            System.Threading.Thread.Sleep(1000);
            LoadAllProduct();
        }
        else if (DropDownListSoryBy.SelectedIndex == 1)
        {
            //TOP PRICE LOW TO HIGH
            System.Threading.Thread.Sleep(1000);
            DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daProduct.Fill5(ds2.tbl_product);
            DataTable dt = new DataTable();
            dt = ds2.Tables["tbl_product"];

            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = dt.DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }
        else if (DropDownListSoryBy.SelectedIndex == 2)
        {
            //TOP PRICE HIGH TO LOW
            System.Threading.Thread.Sleep(1000);
            DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daProduct.Fill6(ds2.tbl_product);
            DataTable dt = new DataTable();
            dt = ds2.Tables["tbl_product"];

            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = dt.DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }
    }

    protected void drcategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        if (drcategory.SelectedIndex == 0)
        {
            //ALL PRODUCT
            System.Threading.Thread.Sleep(1000);
            LoadAllProduct();
        }
        else
        {
            
            CategoryDAL cateDAL = new CategoryDAL();
            int catID = Convert.ToInt32(cateDAL.GetCategoryID(drcategory.SelectedItem.Text).Rows[0]["cat_id"]);
            LoadProductByCategory(catID);
        }
    }
 
}